/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package onpu_dms_xls.kernel.common.XML_Parsers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import onpu_dms_xls.kernel.common.pdf.PDF_TextLabelData;
import onpu_dms_xls.kernel.common.logAndDebug.Debug;
import onpu_dms_xls.kernel.common.logAndDebug.LoggingFacade;
import onpu_dms_xls.kernel.common.pdf.PDF_Extract_MarkLabels;
import onpu_dms_xls.kernel.common.pdf.PDF_Extract_LayoutTemplate;
import org.apache.log4j.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author snbm
 */
public class PDF_Layout_XML_Parser extends DefaultHandler{
    
    private PDF_Extract_LayoutTemplate res;
    private List<PDF_TextLabelData> textLabelDataArr;
    private String tempVal;

    private PDF_TextLabelData tempTextLabelData;

    private PDF_Layout_XML_Parser () {
        textLabelDataArr = new ArrayList<PDF_TextLabelData>();
        res = new PDF_Extract_LayoutTemplate();
    }
    
    private static PDF_Layout_XML_Parser instance;

    public static PDF_Layout_XML_Parser getInstance(){
        if (instance == null){
            instance = new PDF_Layout_XML_Parser();
        }
        return instance;
    }

    public PDF_Extract_LayoutTemplate run(String xmlFilePath){
        LoggingFacade.trace.info("SAX_PDF_XML_Parser, runExample call with xmlFilePath="+xmlFilePath);
        parseDocument(xmlFilePath);

        res.setTextLabelDataArr(textLabelDataArr);
        return res;
    }

    private void parseDocument(String xmlFilePath){

        SAXParserFactory spf = SAXParserFactory.newInstance();

        try{
            SAXParser sp = spf.newSAXParser();
            sp.parse(xmlFilePath, this);
        } catch (SAXException se) {
            se.printStackTrace(System.out);
            return;
        } catch (ParserConfigurationException pce) {
            pce.printStackTrace(System.out);
            return;
        } catch (IOException ie) {
            ie.printStackTrace(System.out);
            return;
        }
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        tempVal = "";

        if (qName.equalsIgnoreCase("marks")){
            PDF_Extract_MarkLabels mLabels = new PDF_Extract_MarkLabels();
            mLabels.setFont(attributes.getValue("font"));
            mLabels.setFontSize(Integer.parseInt(attributes.getValue("fontSize")));
            mLabels.setCodePage(attributes.getValue("codePage"));
            mLabels.setX(Integer.parseInt(attributes.getValue("x")));
            mLabels.setFirstNameColLeftBorder(Integer.parseInt(attributes.getValue("firstNameColLeftBorder")));
            mLabels.setFirstNameColRightBorder(Integer.parseInt(attributes.getValue("firstNameColRightBorder")));
            mLabels.setStudyHourX(Integer.parseInt(attributes.getValue("studyHourX")));
            mLabels.setMarkX(Integer.parseInt(attributes.getValue("markX")));
            mLabels.setRowHeigh(Double.parseDouble(attributes.getValue("rowHeigh")));
            mLabels.setColumnDelta(Integer.parseInt(attributes.getValue("columnDelta")));
            mLabels.setFirstColFirstLineY(Integer.parseInt(attributes.getValue("firstColFirstLineY")));
            mLabels.setFirstColSecondLineY(Integer.parseInt(attributes.getValue("firstColSecondLineY")));
            mLabels.setSecondAndThirdColFirstLineY(Integer.parseInt(attributes.getValue("secondAndThirdColFirstLineY")));
            mLabels.setSecondAndThirdColSecondLineY(Integer.parseInt(attributes.getValue("secondAndThirdColSecondLineY")));
            mLabels.setBottomY(Integer.parseInt(attributes.getValue("bottomY")));

            res.setMarkLabels(mLabels);
        }

        if (qName.equalsIgnoreCase("textLabel")){
            tempTextLabelData = new PDF_TextLabelData();
            tempTextLabelData.setFont(attributes.getValue("font"));
            tempTextLabelData.setFontSize(Integer.parseInt(attributes.getValue("fontSize")));
            tempTextLabelData.setCodePage(attributes.getValue("codePage"));
            tempTextLabelData.setX(Integer.parseInt(attributes.getValue("x")));
            tempTextLabelData.setY(Integer.parseInt(attributes.getValue("y")));
            if (attributes.getValue("oneYColumnHigh") != null){
                tempTextLabelData.setOneYColumnHigh(Double.parseDouble(attributes.getValue("oneYColumnHigh")));
            }
            if (attributes.getValue("virtColLeftBorder") != null){
                tempTextLabelData.setVirtColLeftBorder(Integer.parseInt(attributes.getValue("virtColLeftBorder")));
            }
            if (attributes.getValue("virtColRightBorder") != null){
                tempTextLabelData.setVirtColRightBorder(Integer.parseInt(attributes.getValue("virtColRightBorder")));
            }
            tempTextLabelData.setPageNum(Integer.parseInt(attributes.getValue("pageNum")));
            
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        tempVal = new String(ch, start, length);
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equalsIgnoreCase("textLabel")){
            tempTextLabelData.setVariableName(tempVal);
            textLabelDataArr.add(tempTextLabelData);
        }
    }
}
